/*
 * MIT License
 *
 * Copyright (c) 2020 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package cn.seaboot.postman;

import cn.seaboot.postman.analyzer.ControllerInfo;
import cn.seaboot.postman.analyzer.MethodInfo;
import cn.seaboot.postman.request.Body;
import cn.seaboot.postman.request.Query;
import cn.seaboot.postman.request.analyzer.*;
import cn.seaboot.postman.response.BodyField;
import cn.seaboot.postman.response.Response;
import cn.seaboot.postman.response.analyzer.BodyFieldAnalyzer;
import cn.seaboot.postman.response.analyzer.DefaultBodyFieldAnalyzer;
import cn.seaboot.postman.response.analyzer.DefaultResponseFactory;
import cn.seaboot.postman.response.analyzer.ResponseFactory;
import cn.seaboot.postman.root.Item;
import cn.seaboot.postman.root.analyzer.ApiAnalyzer;
import cn.seaboot.postman.root.analyzer.DefaultApiAnalyzer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
 * Postman Configuration
 *
 * @author Mr.css
 * @date 2020-01-29 15:29
 */
public class PostmanConfiguration {
    /**
     * postman-collection api Version
     */
    private String postmanVersion = "https://schema.getpostman.com/json/collection/v2.1.0/collection.json";
    /**
     * 采用的网络协议
     */
    private String protocol = "http";
    /**
     * 所有网络请求的基本路径，用于 {@link cn.seaboot.postman.request.Url#setHost(String[])}。
     */
    private String[] host;
    /**
     * API name
     */
    private String apiName = "Doc";
    /**
     * if null, will generate a new guid
     */
    private String postmanId;
    /**
     * 黑名单
     */
    private String[] exclusive;
    /**
     * 白名单
     */
    private String[] include;

    //getter/setter------------------------------------------------------------------

    public String getPostmanVersion() {
        return postmanVersion;
    }

    public void setPostmanVersion(String postmanVersion) {
        this.postmanVersion = postmanVersion;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public String getApiName() {
        return apiName;
    }

    public void setApiName(String apiName) {
        this.apiName = apiName;
    }

    public String getPostmanId() {
        return postmanId;
    }

    public void setPostmanId(String postmanId) {
        this.postmanId = postmanId;
    }

    public String[] getExclusive() {
        return exclusive;
    }

    public void setExclusive(String[] exclusive) {
        this.exclusive = exclusive;
    }

    public String[] getInclude() {
        return include;
    }

    public void setInclude(String[] include) {
        this.include = include;
    }

    public String[] getHost() {
        return host;
    }

    public void setHost(String[] host) {
        this.host = host;
    }

    /**
     * 设置一个基础URL，E.G.: https://www.baidu.com
     *
     * @param url 基础URL
     */
    public void setUrl(String url) {
        this.protocol = url.substring(url.indexOf(':'));
        this.host = new String[]{url.substring(url.indexOf("://") + 3)};
    }

    public void setExclusive(String exclusive) {
        this.exclusive = exclusive.split(";");
    }

    public void setInclude(String include) {
        this.include = include.split(";");
    }

    //默认的扫描接口以及setter---------------------------------------------------------------------------------------

    private QueryAnalyzer analyzeQuery = new DefaultQueryAnalyzer();

    private BodyFieldAnalyzer bodyFieldAnalyzer = new DefaultBodyFieldAnalyzer();

    private ResponseFactory responseFactory = new DefaultResponseFactory();

    private QueryToFormDataConverter queryToFormDataConverter = new DefaultQueryToFormDataConverter();

    private QueryToRequestBodyConverter queryToRequestBodyConverter = new DefaultQueryToRequestBodyConverter();

    private ApiAnalyzer apiAnalyzer = new DefaultApiAnalyzer();

    public void setAnalyzeQuery(QueryAnalyzer analyzeQuery) {
        this.analyzeQuery = analyzeQuery;
    }

    public void setBodyFieldAnalyzer(BodyFieldAnalyzer bodyFieldAnalyzer) {
        this.bodyFieldAnalyzer = bodyFieldAnalyzer;
    }

    public void setResponseFactory(ResponseFactory responseFactory) {
        this.responseFactory = responseFactory;
    }

    public void setQueryToFormDataConverter(QueryToFormDataConverter queryToFormDataConverter) {
        this.queryToFormDataConverter = queryToFormDataConverter;
    }

    public void setQueryToRequestBodyConverter(QueryToRequestBodyConverter queryToRequestBodyConverter) {
        this.queryToRequestBodyConverter = queryToRequestBodyConverter;
    }

    public void setApiAnalyzer(ApiAnalyzer apiAnalyzer) {
        this.apiAnalyzer = apiAnalyzer;
    }

    //-------------------------------------

    /**
     * 扫描所有的查询参数
     *
     * @param method 方法
     * @return 查询参数
     */
    public List<Query> analyzeQuery(MethodInfo method) {
        return this.analyzeQuery.analyzeQuery(this, method);
    }

    /**
     * 查询参数转换为FormData
     *
     * @param methodInfo 方法对象
     * @param queries    查询参数
     * @return Body
     */
    public Body convertToFormData(MethodInfo methodInfo, List<Query> queries) {
        return this.queryToFormDataConverter.convertToFormData(methodInfo, queries);
    }

    /**
     * 查询参数转换为RequestBody
     *
     * @param queries 查询参数
     * @return Body
     */
    public Body convertToRequestBody(List<Query> queries) {
        return this.queryToRequestBodyConverter.convertToRequestBody(this, queries);
    }

    /**
     * 扫描所有的函数返回值
     *
     * @param method 函数
     * @return bodyField
     */
    public BodyField analyzeBodyField(MethodInfo method) {
        return this.bodyFieldAnalyzer.analyzeBodyField(this, method);
    }

    /**
     * 构造返回样例数据
     *
     * @param bodyField ResponseBody
     * @return Response
     */
    public @NotNull List<Response> createResponse(BodyField bodyField) {
        return this.responseFactory.createResponse(bodyField);
    }

    /**
     * 扫描一个Controller所包含的API
     *
     * @param controllerInfo ControllerInfo
     * @return api
     */
    public @Nullable List<Item> searchApiFromController(ControllerInfo controllerInfo) {
        return this.apiAnalyzer.searchApiFromController(this, controllerInfo);
    }
}
